/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeleccionarListado.java
 *
 * Created on 01-may-2011, 22:56:43
 */

package interfaces.farmacia;

import Controlador.Controlador;
import Definiciones.Definiciones.Estado;
import GestorFarmacia.*;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import org.jdesktop.application.Action;

/**
 *
 * @author eslogan
 */
public class SeleccionarListado extends javax.swing.JPanel {

    /** Creates new form SeleccionarListado */
    private ClinicaView app;
    private String tipo_listado = null;
    
    /** Creates new form buscarCita */
    public SeleccionarListado (ClinicaView a) {
        app = a;
        initComponents();
    }

    public void limpiarDatos() {
        fecha_entrada.setDate(null);
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    @Action
    public void aceptar() throws ParseException, IOException, ClassNotFoundException, SQLException {
        int tipoListado;
        boolean error = false;
        Lote [] lotes = null;
        Medicamento [] medica = null;
        ArrayList<Lote> l_lotes = new ArrayList<Lote>();
        DateFormat fecha_formato = new SimpleDateFormat("yyyy-MM-dd");

        app.limpiaErrores();
        Calendar cal_fecha = fecha_entrada.getCalendar();

        if(cal_fecha == null && lotesCad.isSelected()) {
            error = true;
            app.addError("Debes introducir una fecha de caducidad");

        }
        if(error)
            app.muestraListaErrores();
        else {
            if(allStock.isSelected()) {
                lotes = Controlador.listarStock();
                if(lotes == null) {
                    app.limpiaErrores();
                    app.addError("No existen lotes en la BD");
                    app.muestraListaErrores();
                }
                else
                    app.abrirListadoStock(Interfaces.PRINCIPAL_FARMACEUTICO, lotes);
            }
            else if(listPedido.isSelected()) {
                int cantidad;
                lotes = Lote.get(null, null, null, null, null, Estado.DISPONIBLE);
                if(lotes == null) {
                    app.limpiaErrores();
                    app.addError("No existen lotes en la BD");
                    app.muestraListaErrores();
                }
                else {
                    for(int i = 0; i < lotes.length; i++) {
                        cantidad = lotes[i].getMedicamento().getCantidadTipicaMedicamento()-lotes[i].getUnidadesRestantesLote();
                        lotes[i].setUnidadesInicialesLote(cantidad);
                    }
                    app.abrirListadoPedido(Interfaces.PRINCIPAL_FARMACEUTICO, lotes);
                }

            }
            else if(lotesCad.isSelected()) {
                 Date fecha = cal_fecha.getTime();
                 lotes = Controlador.consultarLotesCaducados(fecha);
                 if(lotes == null) {
                    app.limpiaErrores();
                    app.addError("No existen lotes en la BD");
                    app.muestraListaErrores();
                 }
                 else
                    app.abrirListadoCaducados(Interfaces.PRINCIPAL_FARMACEUTICO, lotes, fecha);
            }
            else if(bajasExis.isSelected()) {
                medica = Controlador.consultarBajasExistencias();
                if(medica == null) {
                    app.limpiaErrores();
                    app.addError("No existen lotes en la BD");
                    app.muestraListaErrores();
                }
                else {
                    for(int i = 0; i < medica.length; i++) {
                        l_lotes.addAll(Arrays.asList(Lote.get(medica[i].getIdMedicamento(), null, null, null, null, null)));
                    }
                    lotes = new Lote[l_lotes.size()];
                    l_lotes.toArray(lotes);
                    app.abrirListadoBajasExistencias(Interfaces.PRINCIPAL_FARMACEUTICO, lotes);
                }
            }
            else {
                app.limpiaErrores();
                app.addError("Debe seleccionar un listado de la lista");
                app.muestraListaErrores();
            }
        }
    }

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        app.volver(Interfaces.PRINCIPAL_FARMACEUTICO);
    }

    @Action
    public void noCal(){
        fecha.setVisible(false);
    }

    @Action
    public void siCal(){
        fecha.setVisible(true);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        allStock = new javax.swing.JRadioButton();
        listPedido = new javax.swing.JRadioButton();
        lotesCad = new javax.swing.JRadioButton();
        bajasExis = new javax.swing.JRadioButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        fecha = new javax.swing.JPanel();
        fecha_entrada = new com.toedter.calendar.JDateChooser();

        setFont(Interfaz.setFontSize());
        setPreferredSize(new java.awt.Dimension(780, 470));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(SeleccionarListado.class, this);
        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(SeleccionarListado.class);
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setLabel(resourceMap.getString("jButton3.label")); // NOI18N
        jButton3.setName(""); // NOI18N
        jButton3.setSelected(true);

        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setLabel(resourceMap.getString("jButton1.label")); // NOI18N
        jButton1.setName(""); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Listados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N
        jPanel1.setFont(Interfaz.setFontSize());
        jPanel1.setName("jPanel1"); // NOI18N

        allStock.setAction(actionMap.get("noCal")); // NOI18N
        buttonGroup1.add(allStock);
        allStock.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        allStock.setLabel(resourceMap.getString("allStock.label")); // NOI18N
        allStock.setName("allStock"); // NOI18N
        allStock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allStockActionPerformed(evt);
            }
        });

        listPedido.setAction(actionMap.get("noCal")); // NOI18N
        buttonGroup1.add(listPedido);
        listPedido.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        listPedido.setText("Generar listado de pedido");
        listPedido.setLabel(resourceMap.getString("listPedido.label")); // NOI18N
        listPedido.setName("listPedido"); // NOI18N
        listPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listPedidoActionPerformed(evt);
            }
        });

        lotesCad.setAction(actionMap.get("siCal")); // NOI18N
        buttonGroup1.add(lotesCad);
        lotesCad.setFont(new java.awt.Font("Tahoma", 0, 18));
        lotesCad.setLabel(resourceMap.getString("lotesCad.label")); // NOI18N
        lotesCad.setName("lotesCad"); // NOI18N
        lotesCad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lotesCadActionPerformed(evt);
            }
        });

        bajasExis.setAction(actionMap.get("noCal")); // NOI18N
        buttonGroup1.add(bajasExis);
        bajasExis.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        bajasExis.setLabel(resourceMap.getString("bajasExis.label")); // NOI18N
        bajasExis.setName("bajasExis"); // NOI18N
        bajasExis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajasExisActionPerformed(evt);
            }
        });

        jPanel6.setName("jPanel6"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 243, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel7.setName("jPanel7"); // NOI18N

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 412, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(408, 408, 408)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bajasExis)
                    .addComponent(lotesCad)
                    .addComponent(listPedido)
                    .addComponent(allStock))
                .addContainerGap(296, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(175, 175, 175)
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(allStock)
                        .addGap(18, 18, 18)
                        .addComponent(listPedido)
                        .addGap(18, 18, 18)
                        .addComponent(lotesCad)
                        .addGap(18, 18, 18)
                        .addComponent(bajasExis)))
                .addContainerGap())
        );

        fecha.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Seleccione fecha", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N
        fecha.setFont(Interfaz.setFontSize());
        fecha.setName("fecha"); // NOI18N
        fecha.setVisible(false);

        fecha_entrada.setFont(new java.awt.Font("Tahoma", 0, 18));
        fecha_entrada.setName("fecha_entrada"); // NOI18N

        javax.swing.GroupLayout fechaLayout = new javax.swing.GroupLayout(fecha);
        fecha.setLayout(fechaLayout);
        fechaLayout.setHorizontalGroup(
            fechaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
            .addGroup(fechaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fechaLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(fecha_entrada, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(52, Short.MAX_VALUE)))
        );
        fechaLayout.setVerticalGroup(
            fechaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 85, Short.MAX_VALUE)
            .addGroup(fechaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fechaLayout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addComponent(fecha_entrada, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 311, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton3))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void allStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allStockActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_allStockActionPerformed

    private void listPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listPedidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_listPedidoActionPerformed

    private void lotesCadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lotesCadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_lotesCadActionPerformed

    private void bajasExisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bajasExisActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bajasExisActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton allStock;
    private javax.swing.JRadioButton bajasExis;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel fecha;
    private com.toedter.calendar.JDateChooser fecha_entrada;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JRadioButton listPedido;
    private javax.swing.JRadioButton lotesCad;
    // End of variables declaration//GEN-END:variables

}
